package com.share.web.servlet.init;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.share.commons.util.FileUtil;
import com.share.commons.util.LoadFileUtil;
import com.share.mail.MailConfiguraton;
import com.share.web.servlet.base.BaseServlet;

/**
 * 初始化数据
 * 
 * @author Administrator
 * 
 */
public class InitServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	private ServletContext sc;

	public void init() throws ServletException {
		sc = this.getServletContext() ;
		sc.setAttribute("path", sc.getContextPath()) ;
		welcome() ;
		otherinit() ;
		createUploadFolder() ;
		MailConfiguraton.getInstance() ;
	}

	public void destroy() {
		sc = null;
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		
		String type = null != request.getParameter("type")?request.getParameter("type"):"" ;
		
		if(type.equals("gc")) {
			System.gc() ;
			response.getWriter().print("调用JVM垃圾回收器") ;
		} else if(type.equals("invalidate")) {
			request.getSession().invalidate() ;
			response.getWriter().print("已强制注销所以会话") ;
		}
		
	}

	public void welcome() {
		this.log("share系统启动初始化信息...");
	}

	/** 系统信息初始化 */
	public void otherinit() {
		Object[] sysinit = new Object[4];
		sysinit[0] = sysinfo();	/** 系统信息 */
		sysinit[1] = getDBMSMetaData();	/** 数据库配置信息 */
		sc.setAttribute("sysinit", sysinit);
	}

	/** 创建目录 */
	public void createUploadFolder() {
		this.log("创建文章图片存放目录...");
		FileUtil.creDir(this.sc.getRealPath("/upload") + "/pageImgs");
		System.out.println("创建邮件UID存放文件目录");
		FileUtil.creDir(this.sc.getRealPath("/") + "/mailUIDFile");
	}

	/** 系统信息 */
	public static Object[] sysinfo() {
		Properties pros = LoadFileUtil.loadPro("systeminfo.properties");

		Object[] sysinfo = new Object[10];

		// 应用程序名称
		sysinfo[0] = pros.getProperty("project.name");
		// 应用程序版本
		sysinfo[1] = pros.getProperty("project.version");
		// 服务启动时间
		sysinfo[2] = new Date();
		// 操作系统名称
		sysinfo[3] = System.getProperty("os.name");
		// 操作系统版本
		sysinfo[4] = System.getProperty("os.version");
		// JDK厂商
		sysinfo[5] = System.getProperty("java.vm.vendor");
		// JDK版本
		sysinfo[6] = System.getProperty("java.runtime.version");
		// JDK主目录
		sysinfo[7] = System.getProperty("java.home");

		return sysinfo;
	}

	/** 获得数据库配置信息 */
	public static String[] getDBMSMetaData() {
		String[] dbms = new String[10];
		try {
			Properties pros = LoadFileUtil.loadPro("jdbc.properties");
			Class.forName(pros.getProperty("pool.driverClassName"));
			Connection conn = DriverManager.getConnection(pros.getProperty("pool.url"), pros.getProperty("pool.username"), pros.getProperty("pool.password"));
			DatabaseMetaData md = conn.getMetaData();

			// 数据库类型
			dbms[0] = md.getDatabaseProductName();
			// 数据库服务器地址
			dbms[1] = pros.getProperty("pool.ip");
			// 数据库服务器端口
			dbms[2] = pros.getProperty("pool.port");
			// 数据库名称
			dbms[3] = pros.getProperty("pool.database");
			// 用户名
			dbms[4] = pros.getProperty("pool.username");

			conn.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dbms;
	}
}
